import 'package:citiesocial/_include.dart';

class ActivityDetail extends BaseStatefulWidget {
  final ActicityEntity activityEntity;
  final String id;

  ActivityDetail({this.activityEntity, this.id}) : super();

  @override
  State<StatefulWidget> createState() => _ActivityDetailState();
}

class _ActivityDetailState extends BaseState<ActivityDetail> {
  ActicityDetailEntity activityDetailEntity;
  _ActivityDetailState() : super();

  void _loadDetail() async {
    if (widget.activityEntity != null || widget.id != null) {
      LoadingDialog.show(context);
      final equid = await getDeviceId();
      var detail = await HttpManager.activityDetail(
          widget.activityEntity?.activity_id ?? widget.id, equid);
      setState(() {
        activityDetailEntity = detail;
      });
      LoadingDialog.dismiss(context);
    }
  }

  @override
  void initState() {
    Future.delayed(Configs.loadDelay, () {
      _loadDetail();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final view = Scaffold(
      appBar: AppBarUtils.withCart(context,
          title:
              '${widget.activityEntity?.activity_name ?? activityDetailEntity?.activity_name}'),
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          children: <Widget>[
            // TabTitle(),
            Expanded(
              child: ListView(
                children: _generateChildren(),
              ),
            )
          ],
        ),
      ),
    );
    return view;
  }

  List<Widget> _generateChildren() {
    final views = new List<Widget>();
    if (activityDetailEntity != null) {
      views.add(BannerImage(activityDetailEntity.img));
      views.add(Container(
        child: Center(
          child: Text(
            activityDetailEntity.brand_name ?? '',
            style: TextStyle(fontSize: 18.0, color: Colors.grey),
          ),
        ),
        margin: EdgeInsets.only(top: 10.0, left: 10.0, right: 10.0),
      ));
      views.add(Container(
        child: Center(
          child: Text(
            activityDetailEntity.activity_name ?? '',
            style: TextStyle(fontSize: 20.0, color: Colors.black),
          ),
        ),
        margin: EdgeInsets.only(top: 10.0, left: 10.0, right: 10.0),
      ));
      views.add(Container(
        child: Center(
          child: Text(
            activityDetailEntity.activity_range_ext ?? '',
            style: TextStyle(fontSize: 16.0, color: Colors.grey),
          ),
        ),
        margin: EdgeInsets.only(top: 10.0, left: 10.0, right: 10.0),
      ));
      views.add(Container(
        child: Center(
          child: Text(activityDetailEntity.content ?? ''),
        ),
        margin: EdgeInsets.all(10.0),
      ));
      if (activityDetailEntity.goods_list != null) {
        for (var goods in activityDetailEntity.goods_list) {
          views.add(SquareGoodsItem(goods));
        }
      }
    }
    return views;
  }
}
